<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.cp_id ? '编辑' : '添加'" v-model="visible" align-center width="800px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="供应商">
          <VSelect v-model="form.supplier_id" placeholder="请选择" type="supplier" class="w-full" />
        </el-form-item>
        <el-form-item label="部门">
          <VCascader v-model="form.department_id" placeholder="请选择" type="1" checkStrictly class="w-full" />
        </el-form-item>
        <el-form-item label="结算方式" prop="period_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-radio-group v-model="form.period_id">
            <el-radio v-for="(d, i) in options.period_id" :key="i" :value="d.value">{{ d.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="货款所属期" prop="month" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-date-picker v-model="form.month" placeholder="日期选择" type="month" value-format="YYYY-MM" class="w-full" />
        </el-form-item>
        <div class="grid grid-cols-2 gap-x-3">
          <el-form-item label="币种" prop="currency" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
            <VSelect v-model="form.currency" placeholder="币种" :options="options.currency" filterable clearable class="w-full" />
          </el-form-item>
          <el-form-item label="货款总额" prop="total_price" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
            <el-input-number v-model="form.total_price" controls-position="right" :min="0" :step="0.01" :precision="2" class="w-full" />
          </el-form-item>
        </div>
        <el-form-item label="需求付款日期" prop="except_payment_time" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-date-picker v-model="form.except_payment_time" placeholder="日期选择" type="date" value-format="YYYY-MM-DD" class="w-full" />
        </el-form-item>
        <el-form-item label="付款主体" prop="company_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.company_id" placeholder="付款主体" :options="options.company_id" filterable clearable class="w-full" />
        </el-form-item>
      </div>
      <el-form-item label="备注">
        <el-input v-model="form.remark" placeholder="备注" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

const emit = defineEmits(['close', 'success'])

const form = ref({})
const formRef = ref()

// 打开弹窗
const open = async (cp_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = { period_id: 1 }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
  if(cp_id) {
    api.detail({cp_id}).then(res => {
      form.value = res.data
    })
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async () => {
  if (!await formRef?.value.validate()) return
  loadings.submit = true
  api[form.value.cp_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>